package com.sys.web.head.controller;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sys.common.controller.BaseController;
import com.sys.common.pojo.ApiResponse;
import com.sys.common.pojo.ApiResponseEnum;
import com.sys.common.util.ConstatFinalUtil;
import com.sys.users.pojo.Customer;
import com.sys.users.pojo.ScenicSpotAdmin;
import com.sys.users.pojo.SuperSsa;
import com.sys.users.service.IUsersDbService;

/**
 * 	不需要登陆的Controller
 * 	@author 梦缠绵
 */
@Controller
public class NoLoginHeadController extends BaseController
{
	@Resource
	private IUsersDbService usersDbService;
	
	/**
	 * 打开登陆页面
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/login")
	public String login(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		ConstatFinalUtil.SYS_LOGGER.info("--login--");
		Customer users = (Customer) this.findObjfromSession(request, "customer");
		if(users != null)
		{
			/* 登陆成功 */
			return "redirect:/head/users/main.htm" ; 
		}
		return "redirect:/main.htm";
	}
	
	/**
	 * 登陆提交操作
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/loginSubmit")
	public String loginSubmit(HttpServletRequest request,HttpSession session , HttpServletResponse response,Model model) throws IOException
	{
		ConstatFinalUtil.SYS_LOGGER.info("--loginSubmit--");
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		/* 存储多个结果,Map存储 */
		Map<String, Object> dataMapJava = new HashMap<String, Object>();
		apiResponse.setDataMapJava(dataMapJava);

		/* 接收参数 */
		String account = request.getParameter("account");
		String password = request.getParameter("password");
		/* 1:普通用户;2:景点管理员;3:普通管理员 */
		String userType = request.getParameter("userType");
		session.setAttribute("userType", userType);
		
		String returnUrl = "";
		
		if("1".equalsIgnoreCase(userType))
		{
			returnUrl = loginCustomer(request, apiResponse, account, password);
		}else if("2".equalsIgnoreCase(userType))
		{
			returnUrl = loginScenicspotadmin(request, apiResponse, account, password);
		}else if("3".equalsIgnoreCase(userType))
		{
			returnUrl = loginSuperssa(request, apiResponse, account, password);
		}
		
		if(!"".equalsIgnoreCase(returnUrl))
		{
			/* 登陆成功 */
			return returnUrl ; 
		}
		/* 提示信息 */
		model.addAttribute("response", apiResponse.toJSON());
		return this.login(request, response);
	}

	/**
	 * 超级管理员
	 * @param request
	 * @param apiResponse
	 * @param account
	 * @param password
	 * @return
	 */
	private String loginSuperssa(HttpServletRequest request, ApiResponse<Object> apiResponse, String account,
			String password)
	{
		HttpSession session = request.getSession() ; 
		/* 查询db,处理业务逻辑 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("account", account);
		ApiResponse<SuperSsa> apiResponseRes = this.usersDbService.findOneSuperSsaService(condMap);
		SuperSsa superSsa = apiResponseRes.getDataOneJava();
		/* 检验邮箱 */
		if (superSsa != null)
		{
			/* 检验密码 */
			if (this.encryptUtil.checkStr(password, superSsa.getPassword()))
			{
				/* 往session中存储数据 */
				session.setAttribute("superSsa",superSsa);

				/* 登陆成功 */
				apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
				apiResponse.setInfo(
						ConstatFinalUtil.INFO_JSON.getString(ApiResponseEnum.STATUS_SUCCESS.getStatus() + ""),
						Collections.EMPTY_MAP);
				/* 登陆成功 */
				return "redirect:/head/users/main.htm";
			} else
			{
				/* 密码不正确 */
				apiResponse.setCode(ApiResponseEnum.INFO_PASS_INCORRECT.getStatus());
				apiResponse.setInfo(
						ConstatFinalUtil.INFO_JSON.getString(ApiResponseEnum.INFO_PASS_INCORRECT.getStatus() + ""),
						Collections.EMPTY_MAP);
			}
		}else
		{
			/* 邮箱不存在 */
			apiResponse.setCode(ApiResponseEnum.INFO_EMAIL_NOT_EXISTS.getStatus());
			apiResponse.setInfo(
					ConstatFinalUtil.INFO_JSON.getString(ApiResponseEnum.INFO_EMAIL_NOT_EXISTS.getStatus() + ""),
					Collections.EMPTY_MAP);
		}
		return "" ; 
	}

	/**
	 * 景点管理员
	 * @param request
	 * @param apiResponse
	 * @param account
	 * @param password
	 * @return
	 */
	private String loginScenicspotadmin(HttpServletRequest request, ApiResponse<Object> apiResponse, String account,
			String password)
	{
		HttpSession session = request.getSession() ; 
		/* 查询db,处理业务逻辑 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("account", account);
		ApiResponse<ScenicSpotAdmin> apiResponseRes = this.usersDbService.findOneScenicSpotAdminService(condMap);
		ScenicSpotAdmin scenicSpotAdmin = apiResponseRes.getDataOneJava();
		/* 检验邮箱 */
		if (scenicSpotAdmin != null)
		{
			/* 检验密码 */
			if (this.encryptUtil.checkStr(password, scenicSpotAdmin.getPassword()))
			{
				/* 往session中存储数据 */
				session.setAttribute("scenicSpotAdmin",scenicSpotAdmin);

				/* 登陆成功 */
				apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
				apiResponse.setInfo(
						ConstatFinalUtil.INFO_JSON.getString(ApiResponseEnum.STATUS_SUCCESS.getStatus() + ""),
						Collections.EMPTY_MAP);
				/* 登陆成功 */
				return "redirect:/head/users/main.htm";
			} else
			{
				/* 密码不正确 */
				apiResponse.setCode(ApiResponseEnum.INFO_PASS_INCORRECT.getStatus());
				apiResponse.setInfo(
						ConstatFinalUtil.INFO_JSON.getString(ApiResponseEnum.INFO_PASS_INCORRECT.getStatus() + ""),
						Collections.EMPTY_MAP);
			}
		}else
		{
			/* 邮箱不存在 */
			apiResponse.setCode(ApiResponseEnum.INFO_EMAIL_NOT_EXISTS.getStatus());
			apiResponse.setInfo(
					ConstatFinalUtil.INFO_JSON.getString(ApiResponseEnum.INFO_EMAIL_NOT_EXISTS.getStatus() + ""),
					Collections.EMPTY_MAP);
		}
		return "" ; 
	}

	/**
	 * 普通用户登陆
	 * @param session
	 * @param apiResponse
	 * @param dataMapJava
	 * @param account
	 * @param password
	 */
	private String loginCustomer(HttpServletRequest request, ApiResponse<Object> apiResponse, 
			String account, String password)
	{
		HttpSession session = request.getSession() ; 
		/* 查询db,处理业务逻辑 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("account", account);
		ApiResponse<Customer> apiResponseRes = this.usersDbService.findOneCustomerService(condMap);
		Customer customer = apiResponseRes.getDataOneJava();
		/* 检验邮箱 */
		if (customer != null)
		{
			/* 检验密码 */
			if (this.encryptUtil.checkStr(password, customer.getPassword()))
			{
				/* 往session中存储数据 */
				session.setAttribute("customer",customer);

				/* 登陆成功 */
				apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
				apiResponse.setInfo(
						ConstatFinalUtil.INFO_JSON.getString(ApiResponseEnum.STATUS_SUCCESS.getStatus() + ""),
						Collections.EMPTY_MAP);
				/* 登陆成功 */
				return "redirect:/head/users/main.htm";
			} else
			{
				/* 密码不正确 */
				apiResponse.setCode(ApiResponseEnum.INFO_PASS_INCORRECT.getStatus());
				apiResponse.setInfo(
						ConstatFinalUtil.INFO_JSON.getString(ApiResponseEnum.INFO_PASS_INCORRECT.getStatus() + ""),
						Collections.EMPTY_MAP);
			}
		}else
		{
			/* 邮箱不存在 */
			apiResponse.setCode(ApiResponseEnum.INFO_EMAIL_NOT_EXISTS.getStatus());
			apiResponse.setInfo(
					ConstatFinalUtil.INFO_JSON.getString(ApiResponseEnum.INFO_EMAIL_NOT_EXISTS.getStatus() + ""),
					Collections.EMPTY_MAP);
		}
		return "" ; 
	}
	
	/**
	 * 打开注册页面
	 * @return
	 */
	@RequestMapping("/register")
	public String register()
	{
		ConstatFinalUtil.SYS_LOGGER.info("--register--");
		return "redirect:/main.htm";
	}
	
	/**
	 * 注册提交操作
	 * @return
	 */
	@RequestMapping("/registerSubmit")
	public String registerSubmit(HttpServletRequest request,HttpSession session , Model model)
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>() ; 
		ConstatFinalUtil.SYS_LOGGER.info("--registerSubmit--");
		
		/* 接收参数 */
		String account = request.getParameter("account");
		String password = request.getParameter("password");
		
		/* 创建对象 */
		Customer customer = new Customer();
		/* 赋值 */
		customer.setAccount(account);
		customer.setPassword(password);
		
		apiResponse = this.usersDbService.saveOneCustomerService(customer);
		ConstatFinalUtil.SYS_LOGGER.info("==注册成功=={}", apiResponse.toJSON());
		if (apiResponse.getCode() == ApiResponseEnum.STATUS_SUCCESS.getStatus())
		{
			apiResponse.setInfo(
					ConstatFinalUtil.INFO_JSON.getString(ApiResponseEnum.INFO_REGISTER_SUCCESS.getStatus() + ""),
					Collections.EMPTY_MAP);
		}
		/* 提示信息 */
		model.addAttribute("response", apiResponse.toJSON());
		return this.register();
	}
}
